package com.ehotting.edsta.circuitDesign.controller;

import com.alibaba.fastjson.JSONObject;
import com.ehotting.eaf.core.web.controller.BaseController;
import com.ehotting.eaf.core.web.domain.AjaxResult;
import com.ehotting.eaf.core.web.page.TableDataInfo;
import com.ehotting.edsta.circuitDesign.domain.EsCircuit;
import com.ehotting.edsta.circuitDesign.domain.EsCircuitPcb;
import com.ehotting.edsta.circuitDesign.service.EsCircuitPcbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/circuitPcb")
@Slf4j
public class EsCircuitPcbController extends BaseController {

    @Value(value = "${circuitFilesIp}")
    private String circuitFilesIp;
    @Autowired
    private EsCircuitPcbService esCircuitPcbService;

    @GetMapping("/list")
    public TableDataInfo list(EsCircuitPcb esCircuitPcb)
    {
        startPage();
        List<EsCircuitPcb> list = esCircuitPcbService.selectEsCircuitPcbs(esCircuitPcb);
        return getDataTable(list);
    }

    @GetMapping("/allList")
    public AjaxResult allList()
    {
        List<EsCircuitPcb> list = esCircuitPcbService.selectEsCircuitPcbs(null);
        return AjaxResult.success(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id, HttpServletRequest request)
    {
        String authorization = request.getHeader("Authorization");
        String newToken = "&token="+authorization.replace("Bearer ","");
        EsCircuitPcb esCircuitPcb = esCircuitPcbService.selectEsCircuitPcbById(id);
        esCircuitPcb.setImage(circuitFilesIp + esCircuitPcb.getImage() + newToken);
        return AjaxResult.success(esCircuitPcb);
    }

    @PostMapping
    public AjaxResult add(@RequestBody EsCircuitPcb esCircuitPcb)
    {

        return toAjax(esCircuitPcbService.insertEsCircuitPcb(esCircuitPcb));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody EsCircuitPcb esCircuitPcb)
    {
        return toAjax(esCircuitPcbService.updateEsCircuitPcb(esCircuitPcb));
    }

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(esCircuitPcbService.deleteEsCircuitPcbByIds(ids));
    }


}
